#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import datetime
import os
import time
import datetime
import sys
#import controller.sessions.SessionManager
#from appengine_utilities.sessions import Session
from lib.gaesessions import get_current_session
#from controller.appengine_utilities.flash import Flash
#from controller.appengine_utilities.cache import Cache
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import datastore_errors
from google.appengine.ext.webapp import template
from model.models import *
import appdir

class PSHandler(webapp.RequestHandler):
  
	def __init__(self):
	    pass
	
	def start(self):
	    self.flash = None
	    self.flash_type = 'normal'
	    self.log_count = 1
	    self.session = get_current_session()
	    self.tplval = {}
	    if self.session.has_key('current_user'):
	      self.user = self.session['current_user']
	    else:
	      self.user = PSUser.all().filter('email = ','leonardo@luarte.net').get()
	    self.logged_user = self.user
	    try:
	      self.current_company = self.user.area.company
	      self.current_area = self.user.area
	      self.add("current_company",self.current_company)
	      self.add("current_area",self.current_area)
	    except:
	      pass
	    self.add("logged_user",self.user)
	
	
	
	def set_flash(self,flash,flash_type='normal'):
		if(self.session):
			self.session['flash'] = flash
			self.session['flash_type'] = flash_type
	
	def read_flash(self):
		if(self.session):
			if(self.session.has_key('flash')):
				self.flash = self.session.pop('flash',default=None)
				self.flash_type = self.session.pop('flash_type',default='normal')
	
	def wr(self,text):
	    self.response.out.write(text)
	
	def wl(self,text):
	    self.response.out.write("<p>" + str(text)+"</p>")
	
	def param(self,param_name):
	  return self.request.get(param_name)
	
	def auth_check(self):
	    
	    self.start()
	    return True
	def add(self,key,value):
	  self.tplval[key] = value
	
	def render(self,view="default_view",template_values={}):
	  
	  self.read_flash()
	  template_values.update(self.tplval)
	  
	  template_values["flash"] = self.flash
	  template_values["flash_type"] = self.flash_type
	  
	  path = os.path.join(os.path.dirname(__file__), '../views/'+view+'.html')
	  self.response.out.write(template.render(path, template_values))
	
	def get(self):
		self.auth_check()
		self.internal_get()
	
	def post(self):
		
		self.auth_check()
		self.internal_post()
	 
